/**
 * 
 */
package com.yunyike.controller.payment;

import java.math.BigDecimal;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CartConstants;
import com.yunyike.constants.PaymentConstants;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.exception.BadRequestException;
import com.yunyike.model.Orders;
import com.yunyike.model.Payment;
import com.yunyike.service.IOrderService;
import com.yunyike.service.IPaymentService;
import com.yunyike.utils.UserUtils;

/**
 * @author zhengqiang.shi
 * 2015年12月16日 下午3:39:02
 */
@Controller
@RequestMapping(value=RequestConstant.PAYMENT)
public class PaymentController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(PaymentController.class);
	
	@Autowired
	private IPaymentService paymentService;
	@Autowired
	private IOrderService orderService;
	/**
	 * 结算页选择支付方式
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.PAYMENT_CHOOSE+"/{payType}")
	public String payTypeChoose(@PathVariable Integer payType,
			HttpServletRequest request, HttpServletResponse response){
	
		log.info("choose pay type :"+payType);
		if(payType == null || !Arrays.asList(1,2).contains(payType)){
			payType = PaymentConstants.PAY_TYPE_2;
		}
		
		String sessionId = UserUtils.getSessionId(request, response);
		
		Payment payment = new Payment();
		payment.setPaymentType(payType);
		paymentService.choosePaymentType(payment,sessionId);
		
		log.info("choose pay type end.");
		return null;
	}
	
	/**
	 * 选择支付方式
	 */
	@RequestMapping(value=RequestConstant.PAYMENT_SELECT+"/{orderId}/{payType}")
	public String payTypeChoose(@PathVariable String orderId,@PathVariable Integer payType,
			HttpServletRequest request, HttpServletResponse response, Model model){
	
		log.info("to select pay type orderId"+orderId+"\tpayType:"+payType);
		if(StringUtils.isBlank(orderId)){
			throw new BadRequestException();
		}
		Orders order = orderService.selectByPrimaryKey(orderId);
		BigDecimal price = new BigDecimal(order.getOrderPrice());
		BigDecimal totalCount = (price.divide(CartConstants.SCANCODE_MERCHANDISE_PRICE,0, BigDecimal.ROUND_HALF_UP));
		/*
		String sessionId = UserUtils.getSessionId(request, response);
		
		Payment payment = new Payment();
		payment.setPaymentType(payType);
		paymentService.choosePaymentType(payment,sessionId);*/
		model.addAttribute("shopId", order.getShopId());
		model.addAttribute("payType", payType);
		model.addAttribute("totalCount", totalCount.intValue());
		model.addAttribute("price", price);
		log.info("choose pay type end.");
		return ResponseConstant.PAY_SELECT;
	}
}
